class AutoUsersController < ApplicationController
  layout "main"
  before_filter :pre_session
  active_scaffold :auto_user do |config|
    config.label = '<br/>'
    config.list.per_page = 10
    config.actions = [:search, :show, :list, :update, :delete]
    config.columns = [:id, :login, :gender, :title, :fname, :lname, :e_title, :e_fname, :e_lname, :nid, :dob, :education, :org, :position, :sport, :user_type, :email, :address, :province, :zip, :phone, :cellphone, :image, :show_image, :role, :facebook_id]
    config.columns[:province].form_ui = :select
    config.columns[:province].options = {:options => Province.find(:all, :order => "th" ).map {|p| [p.th, p.th] }}
    config.columns[:title].form_ui = :select
    config.columns[:title].options = {:options => UserTitle.find(:all, :order => "name" ).map {|ut| [ut.name, ut.name] }}
    config.columns[:gender].form_ui = :select
    config.columns[:gender].options = {:options => %w[ชาย หญิง]}
    config.columns[:education].form_ui = :select
    config.columns[:education].options = {:options => Education.find(:all, :order => "name" ).map {|e| [e.name, e.id] }}
    config.columns[:user_type].form_ui = :select
    config.columns[:user_type].options = {:options => UserType.find(:all, :order => "name" ).map {|ut| [ut.name, ut.id] }}
    config.columns[:gender].label = "เพศ"
    config.columns[:title].label = "คำนำหน้าชื่อ"
    config.columns[:fname].label = "ชื่อ"
    config.columns[:lname].label = "นามสกุล"
    config.columns[:e_title].label = "คำนำหน้าชื่อภาษาอังกฤษ(Title)"
    config.columns[:e_fname].label = "ชื่อภาษาอังกฤษ(Firstname)"
    config.columns[:e_lname].label = "นามสกุลภาษาอังกฤษ(Lastname)"
    config.columns[:nid].label = "เลขประจำตัวประชาชน"
    config.columns[:dob].label = "วันเกิด(yyyy-mm-dd)"
    config.columns[:education].label = "การศึกษา"
    config.columns[:org].label = "หน่วยงาน/บริษัท"
    config.columns[:position].label = "ตำแหน่ง/อาชีพ"
    config.columns[:user_type].label = "ประเภทผู้ใช้งาน"    
    config.columns[:address].label = "ที่อยู่"
    config.columns[:province].label = "จังหวัด"
    config.columns[:zip].label = "รหัสไปรษณีย์"
    config.columns[:sport].label = "กีฬา"
    config.columns[:role].label = "ระดับสิทธิ์"
    config.update.columns.exclude :id, :login, :show_image
    config.list.columns.exclude :e_title, :e_fname, :e_lname, :nid, :dob, :education, :org, :position, :sport, :user_type, :email, :address, :zip, :province, :phone, :cellphone, :image, :show_image
    config.list.sorting = {:id => 'ASC'}
    config.action_links.add 'reset_password', :label => 'ResetPass', :type => :member
  end

  def reset_password
    u = User.find(params[:id])
    u.password = Digest::SHA1.hexdigest(u.login)
    u.save
    render :text=>"Done!"
  end

  def pre_session
    if !isadmin?
      redirect_to :controller=>"main"
    end
  end
end
